package itbour.onetouchshow.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by zjl_d on 2017/12/6.
 * 虚线边框控件
 */

public class InchingDottedView extends View {

    private Paint paint;

    public InchingDottedView(Context context) {
        this(context, null);
    }

    public InchingDottedView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InchingDottedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.parseColor("#16D3CD")); //设置画笔颜色
        paint.setStrokeWidth(UIUtils.dp2px(1));//设置画笔宽度
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        //  画外边框的控制线
        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        paint.setPathEffect(effects);
//            横向第一条虚线
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, canvas.getHeight());
        path.lineTo(canvas.getWidth(), canvas.getHeight());
        path.lineTo(canvas.getWidth(), 0);
        path.close();
        canvas.drawPath(path, paint);  //同一个path对象，只需要一个drawPath()
    }

    public void refreshInchingDottedLineView(int w, int h) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) (this).getLayoutParams();
        layoutParams.width = w;
        layoutParams.height = h;
        this.setLayoutParams(layoutParams);
        this.postInvalidate();
    }
}
